/**
 * Created by Administrator on 2017/3/1 0001.
 */
var Scroller={
    create:function (target,pageWidth,pageHeight,pageDir,_name,_arrPage) {
        var sc={pageIndex:0,pageMax:_arrPage.length-1};
        sc.name=_name;
        sc.arrPage=_arrPage;
        var bool_down;

        var mouseDownPT={x:0,y:0};
        var targetStartPT={x:0,y:0};
        var threhold=100;
        var tw_page;

        target.interactive=true;
        target.on('pointerdown',downHandler);
        target.on('pointerup', upHandler)
        target.on('pointermove',moveHandler);

        sc.scrollToPage=function (pageIdx) {
            sc.pageIndex=pageIdx;
            if(sc.dir=='hor')
            {
                target.x=-sc.pageIndex*pageWidth;
            }
            else
            {
                target.y=-sc.pageIndex*pageHeight;
            }

        }
        sc.scrollToPosition=function (x,y) {

        }


        function downHandler(e) {
            bool_down=true;
            if (tw_page) {
                tw_page.stop();
                TWEEN.remove(tw_page);
                tw_page=null;
            }
            mouseDownPT.x = e.data.global.x;
            mouseDownPT.y = e.data.global.y;
            targetStartPT.x = target.x;
            targetStartPT.y = target.y;
        }
        function upHandler(e) {
            bool_down = false;
            if(pageDir=='ver') {
                if (target.y < -sc.pageIndex * pageHeight - threhold) {
                    if (sc.pageIndex != sc.pageMax) {
                        target.interactive = false;
                        var event = new CustomEvent(sc.name+"_ScrollStart", {detail:sc.pageIndex });
                        document.dispatchEvent(event);
                        // if (tw_page) {
                        //     tw_page.stop();
                        //     TWEEN.remove(tw_page);
                        //     tw_page = null;
                        // }
                        // tw_page = new TWEEN.Tween({y: target.y})
                        //     .to({y: -pageHeight * (sc.pageIndex + 1)}, 700)
                        //     .easing(TWEEN.Easing.Back.Out)
                        //     .onUpdate(function () {
                        //         target.y = this.y;
                        //     })
                        //     .onComplete(function () {
                        //         sc.pageIndex++;
                        //         target.interactive = true;
                        //         var event = new CustomEvent("Game_ScrollComplete", {detail:sc.pageIndex });
                        //         document.dispatchEvent(event);
                        //     })
                        //     .start();
                        TweenMax.to(target,0.7,{y:-pageHeight * (sc.pageIndex + 1),ease:Back.easeOut,onComplete:function () {
                            sc.pageIndex++;
                            target.interactive = true;
                            var event = new CustomEvent(sc.name+"_ScrollComplete", {detail: sc.pageIndex});
                            document.dispatchEvent(event);
                        }});
                    }
                    else {
                        back();
                    }

                }
                else if (target.y > -sc.pageIndex * pageHeight + threhold) {
                    if (sc.pageIndex != 0) {
                        target.interactive = false;
                        var event = new CustomEvent(sc.name+"_ScrollStart", {detail:sc.pageIndex });
                        document.dispatchEvent(event);
                        // if (tw_page) {
                        //     tw_page.stop();
                        //     TWEEN.remove(tw_page);
                        //     tw_page = null;
                        // }
                        // tw_page = new TWEEN.Tween({y: target.y})
                        //     .to({y: -pageHeight * (sc.pageIndex - 1)}, 700)
                        //     .easing(TWEEN.Easing.Back.Out)
                        //     .onUpdate(function () {
                        //         target.y = this.y;
                        //     })
                        //     .onComplete(function () {
                        //         sc.pageIndex--;
                        //         target.interactive = true;
                        //         var event = new CustomEvent("Game_ScrollComplete", {detail:sc.pageIndex });
                        //         document.dispatchEvent(event);
                        //     })
                        //     .start();
                        TweenMax.to(target,0.7,{y:-pageHeight * (sc.pageIndex - 1),ease:Back.easeOut,onComplete:function () {
                            sc.pageIndex--;
                            target.interactive = true;
                            var event = new CustomEvent(sc.name+"_ScrollComplete", {detail: sc.pageIndex});
                            document.dispatchEvent(event);
                        }});
                    }
                    else {
                        back();
                    }
                }

                else {
                    back();
                }
            }
            else if(pageDir=='hor')
            {

                if (target.x < -sc.pageIndex * pageWidth - threhold) {
                    if (sc.pageIndex != sc.pageMax) {
                        target.interactive = false;
                        var event = new CustomEvent(sc.name+"_ScrollStart", {detail:sc.pageIndex });
                        // document.dispatchEvent(event);
                        // if (tw_page) {
                        //     tw_page.stop();
                        //     TWEEN.remove(tw_page);
                        //     tw_page = null;
                        // }

                        // tw_page = new TWEEN.Tween({x: target.x})
                        //     .to({x: -pageWidth * (sc.pageIndex + 1)}, 700)
                        //     .easing(TWEEN.Easing.Back.Out)
                        //     .onUpdate(function () {
                        //         target.x = this.x;
                        //     })
                        //     .onComplete(function () {
                        //         sc.pageIndex++;
                        //         target.interactive = true;
                        //         var event = new CustomEvent("Game_ScrollComplete", {detail:sc.pageIndex });
                        //         document.dispatchEvent(event);
                        //     })
                        //     .start();
                        TweenMax.to(target,0.7,{x:-pageWidth * (sc.pageIndex + 1),ease:Back.easeOut,onComplete:function () {
                            sc.pageIndex++;
                            target.interactive = true;
                            var event = new CustomEvent(sc.name+"_ScrollComplete", {detail: sc.pageIndex});
                            document.dispatchEvent(event);
                        }});
                    }
                    else {
                        back();
                    }

                }
                else if (target.x > -sc.pageIndex * pageWidth + threhold) {
                    if (sc.pageIndex != 0) {
                        target.interactive = false;
                        var event = new CustomEvent(sc.name+"_ScrollStart", {detail:sc.pageIndex });
                        document.dispatchEvent(event);
                        // if (tw_page) {
                        //     tw_page.stop();
                        //     TWEEN.remove(tw_page);
                        //     tw_page = null;
                        // }
                        // tw_page = new TWEEN.Tween({x: target.x})
                        //     .to({x: -pageWidth * (sc.pageIndex - 1)}, 700)
                        //     .easing(TWEEN.Easing.Back.Out)
                        //     .onUpdate(function () {
                        //         target.x = this.x;
                        //     })
                        //     .onComplete(function () {
                        //         sc.pageIndex--;
                        //         target.interactive = true;
                        //         var event = new CustomEvent("Game_ScrollComplete", {detail:sc.pageIndex });
                        //         document.dispatchEvent(event);
                        //     })
                        //     .start();
                        TweenMax.to(target,0.7,{x:-pageWidth * (sc.pageIndex - 1),ease:Back.easeOut,onComplete:function () {
                            sc.pageIndex--;
                            target.interactive = true;
                            var event = new CustomEvent(sc.name+"_ScrollComplete", {detail:sc.pageIndex });
                            document.dispatchEvent(event);
                        }});
                    }
                    else {
                        back();
                    }
                }

                else {
                    back();
                }
            }
        }
        function moveHandler(e) {

            if (bool_down) {
                if(pageDir=='ver')
                {
                    var mouseY=e.data.global.y;
                    if (sc.pageIndex == 0 && mouseY > mouseDownPT.y) {
                        target.y=targetStartPT.y+(mouseY - mouseDownPT.y)/4;
                    }
                    else if (sc.pageIndex == sc.pageMax && mouseY < mouseDownPT.y) {
                        target.y=targetStartPT.y+(mouseY-mouseDownPT.y)/4;
                    }
                    else {
                        target.y=targetStartPT.y+(mouseY-mouseDownPT.y);
                    }
                }
                else if(pageDir=='hor')
                {
                    var mouseX=e.data.global.x;
                    if (sc.pageIndex == 0 && mouseX > mouseDownPT.x) {
                        target.x=targetStartPT.x+(mouseX - mouseDownPT.x)/4;
                    }
                    else if (sc.pageIndex == sc.pageMax && mouseX < mouseDownPT.x) {
                        target.x=targetStartPT.x+(mouseX-mouseDownPT.x)/4;
                    }
                    else {
                        target.x=targetStartPT.x+(mouseX-mouseDownPT.x);
                    }
                }
            }
        }
        function back()
        {
            if (tw_page) {
                tw_page.stop();
                TWEEN.remove(tw_page);
                tw_page=null;
            }


            if(pageDir=='ver')
            {

                var time = 1 * Math.abs(target.y - (sc.pageIndex * -pageHeight)) / 400;
                TweenMax.to(target,time,{y:sc.pageIndex*-pageHeight,ease:Back.easeOut,onComplete:function () {
                    target.interactive=true;
                }});
                // var time = 1000 * Math.abs(target.y - (sc.pageIndex * -pageHeight)) / 400;
                // tw_page=new TWEEN.Tween({y:target.y})
                //     .to({y:sc.pageIndex * -pageHeight },time)
                //     .easing(TWEEN.Easing.Back.Out)
                //     .onUpdate(function(){
                //         target.y=this.y;
                //     })
                //     .onComplete(function () {
                //         target.interactive=true;
                //     })
                //     .start();
            }
            else if(pageDir=='hor')
            {
                // var time = 1000 * Math.abs(target.x - (sc.pageIndex * -pageWidth)) / 400;
                // tw_page=new TWEEN.Tween({x:target.x})
                //     .to({x:sc.pageIndex * -pageWidth },time)
                //     .easing(TWEEN.Easing.Back.Out)
                //     .onUpdate(function(){
                //         target.x=this.x;
                //     })
                //     .onComplete(function () {
                //         target.interactive=true;
                //     })
                //     .start();
                var time = 1 * Math.abs(target.x - (sc.pageIndex * -pageWidth)) / 400;
                TweenMax.to(target,time,{x:sc.pageIndex*-pageWidth,ease:Back.easeOut,onComplete:function () {
                    target.interactive=true;
                }});
            }
        }
        return sc;
    }

}